<?
die();
//Data loading
if (GetParam("m","")=="AUDIT")
    $title='INCOME STATEMENT CONSOLIDATION (AUDIT)';
else
    $title='INCOME STATEMENT CONSOLIDATION';

if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. AMPEL JAYA';

$page   = 'PAGE';

$DEFINE_FONT=0;
GetDB();
$DBConnection = new mydb(DB_USER, DB_PASSWORD, ConsDB, DB_HOST);

$pdf=new COAPDFReport("P","mm","Letter");
$pdf->AddPage();

//GET SKADA 60
$Inverse = 1;
CreateDummyCOA ("60");
SummarizeParent ();
$SUBTOTAL_LAST60 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR60 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT60  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY60= $SUBTOTAL_LASTY;

$Inverse = 0;
CreateDummyCOA ("71");
SummarizeParent ();
$SUBTOTAL_LAST71 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR71 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT71  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY71= $SUBTOTAL_LASTY;

$VESSEL_LAST  = $SUBTOTAL_LAST71 + $SUBTOTAL_LAST60;
$VESSEL_CURR  = $SUBTOTAL_CURR71 + $SUBTOTAL_CURR60;
$VESSEL_MUT   = $SUBTOTAL_MUT71  + $SUBTOTAL_MUT60;
$VESSEL_LASTY = $SUBTOTAL_LASTY71 + $SUBTOTAL_LASTY60;

$LeftTree[$LeftIdx]["description"] = "VESSELS GROSS PROFIT: ";
$LeftTree[$LeftIdx]["aku_last"]    = $VESSEL_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $VESSEL_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $VESSEL_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $VESSEL_LASTY;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftIdx++;
$LeftIdx++;

CreateDummyCOA ("72");
SummarizeParent ();
$SUBTOTAL_LAST72 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR72 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT72  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY72= $SUBTOTAL_LASTY;

$OPR_LAST  = $SUBTOTAL_LAST72  + $VESSEL_LAST;
$OPR_CURR  = $SUBTOTAL_CURR72  + $VESSEL_CURR;
$OPR_MUT   = $SUBTOTAL_MUT72   + $VESSEL_MUT;
$OPR_LASTY = $SUBTOTAL_LASTY72 + $VESSEL_LASTY;

$LeftTree[$LeftIdx]["description"] = "OPERATING INCOME: ";
$LeftTree[$LeftIdx]["aku_last"]    = $OPR_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $OPR_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $OPR_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $OPR_LASTY;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftIdx++;
$LeftIdx++;

CreateDummyCOA ("73");
SummarizeParent ();
$SUBTOTAL_LAST73 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR73 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT73  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY73= $SUBTOTAL_LASTY;

$GRS_LAST  = $SUBTOTAL_LAST73  + $OPR_LAST;
$GRS_CURR  = $SUBTOTAL_CURR73  + $OPR_CURR;
$GRS_MUT   = $SUBTOTAL_MUT73   + $OPR_MUT;
$GRS_LASTY = $SUBTOTAL_LASTY73 + $OPR_LASTY;

$LeftTree[$LeftIdx]["description"] = "GROSS PROFIT: ";
$LeftTree[$LeftIdx]["aku_last"]    = $GRS_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $GRS_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $GRS_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $GRS_LASTY;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftIdx++;
$LeftIdx++;

CreateDummyCOA ("80");
SummarizeParent ();
$SUBTOTAL_LAST80 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR80 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT80  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY80= $SUBTOTAL_LASTY;

$IBX_LAST  = $SUBTOTAL_LAST80  + $GRS_LAST;
$IBX_CURR  = $SUBTOTAL_CURR80  + $GRS_CURR;
$IBX_MUT   = $SUBTOTAL_MUT80   + $GRS_MUT;
$IBX_LASTY = $SUBTOTAL_LASTY80 + $GRS_LASTY;

$LeftTree[$LeftIdx]["description"] = "INCOME BEFORE EXTRA ORDINARY ITEMS: ";
$LeftTree[$LeftIdx]["aku_last"]    = $IBX_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $IBX_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $IBX_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $IBX_LASTY;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftIdx++;
$LeftIdx++;

$Inverse = 1;
CreateDummyCOA ("90");
SummarizeParent ();
$SUBTOTAL_LAST90 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR90 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT90  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY90= $SUBTOTAL_LASTY;

$NET_LAST  = $SUBTOTAL_LAST90  + $IBX_LAST;
$NET_CURR  = $SUBTOTAL_CURR90  + $IBX_CURR;
$NET_MUT   = $SUBTOTAL_MUT90   + $IBX_MUT;
$NET_LASTY = $SUBTOTAL_LASTY90 + $IBX_LASTY;

$LeftTree[$LeftIdx]["description"] = "NET INCOME BEFORE TAX: ";
$LeftTree[$LeftIdx]["aku_last"]    = $NET_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $NET_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $NET_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $NET_LASTY;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftIdx++;
$LeftIdx++;


CreateDummyCOA ("91");
SummarizeParent ();
$SUBTOTAL_LAST91 = $SUBTOTAL_LAST;
$SUBTOTAL_CURR91 = $SUBTOTAL_CURR;
$SUBTOTAL_MUT91  = $SUBTOTAL_MUT;
$SUBTOTAL_LASTY91= $SUBTOTAL_LASTY;

$FREETAX_LAST  = $SUBTOTAL_LAST91  + $NET_LAST;
$FREETAX_CURR  = $SUBTOTAL_CURR91  + $NET_CURR;
$FREETAX_MUT   = $SUBTOTAL_MUT91   + $NET_MUT;
$FREETAX_LASTY = $SUBTOTAL_LASTY91 + $NET_LASTY;

$LeftTree[$LeftIdx]["description"] = "NET INCOME: ";
$LeftTree[$LeftIdx]["aku_last"]    = $FREETAX_LAST;
$LeftTree[$LeftIdx]["aku_curr"]    = $FREETAX_CURR;
$LeftTree[$LeftIdx]["mutasi"]      = $FREETAX_MUT;
$LeftTree[$LeftIdx]["lastyear"]    = $FREETAX_LASTY;
$LeftTree[$LeftIdx]["printing"]    = 1;
$LeftTree[$LeftIdx]["garis"]       = 1;
$LeftIdx++;
$LeftIdx++;

$pdf->COATable();
$pdf->Output();

function GetDB() {
    //Connection To Jkt Database
    $DBC1 = new mydb(DB_USER, DB_PASSWORD, OfficeDB1, DB_HOST);
    $DBC2 = new mydb(DB_USER, DB_PASSWORD, OfficeDB2, DB_HOST);
    $DBC3 = new mydb(DB_USER, DB_PASSWORD, ConsDB, DB_HOST);

    $Year   = GetParam("y","");
    $Month  = GetParam("m","");
    if ($Month=="AUDIT") $Month = 12;
    $SQL = "DROP TABLE IF EXISTS `ais_memorial`";
    $DBC3->dbc->query($SQL);

    $SQL = SQLDuplicateStructure("ais_memorial", "ais_memorial", 0, "","memorial_id",$DBC1);
    $DBC3->dbc->query($SQL);

    $session_file = substr(md5(time()), 0, 8);
    $session_file = "/tmp/".$session_file.".tmp";

    $SQL = "SELECT ".GetColumnStruct("ais_memorial", "memorial_id")." FROM ais_memorial WHERE MONTH(tglbkt)=".$Month." AND YEAR(tglbkt)=".$Year."";
    $MyResult = $DBC1->dbc->get_results($SQL,ARRAY_N);
    if ($MyResult) {
        BufferToFile($MyResult,$session_file);

        $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE ais_memorial FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
        $DBC3->dbc->query($SQL);
        unlink($session_file);
    }

    //Connection To Sby Database
    $DBC2 = new mydb(DB_USER, DB_PASSWORD, OfficeDB2, DB_HOST);
    $SQL = "SELECT ".GetColumnStruct("ais_memorial", "memorial_id")." FROM ais_memorial WHERE MONTH(tglbkt)=".$Month." AND YEAR(tglbkt)=".$Year."";
    $MyResult = $DBC2->dbc->get_results($SQL,ARRAY_N);
    if ($MyResult) {
        BufferToFile($MyResult,$session_file);

        $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE ais_memorial FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
        $DBC3->dbc->query($SQL);
        unlink($session_file);
    }

    $SQL = "DROP TABLE IF EXISTS `tblcoa`";
    $DBC3->dbc->query($SQL);

    $SQL = SQLDuplicateStructure("tblcoa", "tblcoa", 0,"","",$DBC1);
    $DBC3->dbc->query($SQL);

    $SQL = "ALTER TABLE tblcoa DROP skada_id";
    $DBC3->dbc->query($SQL);


    $SQL = "SELECT ".GetColumnStruct("tblcoa", "skada_id")." FROM tblcoa WHERE ledger='".$Year."'";
    $MyResult = $DBC1->dbc->get_results($SQL,ARRAY_N);
    if ($MyResult) {
        BufferToFile($MyResult,$session_file);


        $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE tblcoa FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
        $DBC3->dbc->query($SQL);
        unlink($session_file);
    }

    $SQL = "SELECT ".GetColumnStruct("tblcoa", "skada_id")." FROM tblcoa WHERE ledger='".$Year."'";
//  print $SQL;die();
    $MyResult = $DBC2->dbc->get_results($SQL,ARRAY_N);
    if ($MyResult) {
        BufferToFile($MyResult,$session_file);


        $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE tblcoa FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
        $DBC3->dbc->query($SQL);
        unlink($session_file);
    }

}

function CreateDummyCOA ($SkadaRange) {
    global $DBConnection;

    //$TEMP = "TEMPORARY";

    $Year   = GetParam("y","");
    $LastYear = $Year-1;
    $Month  = GetParam("m","");
    if ($Month=="AUDIT") $Month = 13;
    if ($Month<10) $bulan = "0".$Month;
        else $bulan = $Month;

    if (($Month-1)<10) $lastbl = "0".($Month-1);
        else $lastbl = $Month-1;

    if ($Month==1) $LastMonth = "awal ";
    else $LastMonth = "aku".$lastbl;

    $Mutasi = "(mutd".$bulan."-"."mutk".$bulan.") ";
    $CurrentMonth = "aku".$bulan." ";

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp_last`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp_last` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp_last
        SELECT skada, $CurrentMonth, ktrasl
        FROM tblcoa WHERE tipe=1 AND ledger='$LastYear'
    ";

//  if (GetParam("office",""))  $SQL .= "AND ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);


    $SQL = "DROP TABLE IF EXISTS `tblcoatmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
                `total` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `aku_last` decimal( 16,4) NOT NULL,
        `aku_curr` decimal( 16,4) NOT NULL,
        `mutasi` decimal( 16,4) NOT NULL,
                `lastyear` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp
        SELECT tblcoa.skada, tblcoa.pathprn, tblcoa.namacc, tblcoa.parent, tblcoa.printing, jumlah, total, grup, $LastMonth,$CurrentMonth,$Mutasi,akumulasi,tblcoa.ktrasl
        FROM tblcoa LEFT JOIN tblcoatmp_last ON tblcoatmp_last.skada=tblcoa.skada AND tblcoatmp_last.ktrasl=tblcoa.ktrasl
        WHERE tipe=1 AND ledger='$Year'
    ";
    if ($SkadaRange=="60") $SQL .= " AND (tblcoa.skada like '60%') ";
    if ($SkadaRange=="71") $SQL .= " AND (tblcoa.skada like '71%') ";
    if ($SkadaRange=="72") $SQL .= " AND (tblcoa.skada like '72%') ";
    if ($SkadaRange=="73") $SQL .= " AND (tblcoa.skada like '73%') ";
    if ($SkadaRange=="80") $SQL .= " AND (tblcoa.skada like '80%') ";
    if ($SkadaRange=="90") $SQL .= " AND (tblcoa.skada like '90%') ";
    if ($SkadaRange=="91") $SQL .= " AND (tblcoa.skada like '91%') ";

    if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";

    $DBConnection->dbc->query($SQL);
        //$DBConnection->dbc->debug();

    $SQL = "DROP TABLE IF EXISTS `tblcoa_totaltmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoa_totaltmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
                `total` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `aku_last` decimal( 16,4) NOT NULL,
        `aku_curr` decimal( 16,4) NOT NULL,
        `mutasi` decimal( 16,4) NOT NULL,
                `lastyear` decimal( 16,4) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoa_totaltmp
        SELECT
        tblcoatmp.skada, tblcoatmp.pathprn,
        tblcoatmp.namacc, tblcoatmp.parent,
        tblcoatmp.printing, tblcoatmp.jumlah,
                tblcoatmp.total,
        tblcoatmp.grup,
        sum($LastMonth), sum($CurrentMonth), sum($Mutasi), sum(lastyear)
        FROM tblcoatmp, tblcoa
        WHERE tblcoa.pathprn like concat(tblcoatmp.pathprn,'%')
                AND tblcoa.ktrasl=tblcoatmp.ktrasl
    ";
        if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";

    $SQL .= "   GROUP BY tblcoatmp.skada ";
    $DBConnection->dbc->query($SQL);
        //$DBConnection->dbc->debug();
}

function SummarizeParent () {
    global $LeftTree, $DBConnection, $LeftIdx,$SUBTOTAL_LAST,$SUBTOTAL_CURR,$SUBTOTAL_MUT,$Inverse;

    if (!$LeftIdx)  $LeftIdx  = 0;
    $SQL = "
        SELECT skada, namacc, pathprn, parent, printing, jumlah, total, group_name, coatype, aku_last, aku_curr, mutasi, lastyear
        FROM tblcoa_totaltmp, tblcoagroup
        WHERE parent='' AND (coatype='REVENUES' OR coatype='EXPENSES' OR coatype='OTHER EXPENSES') AND tblcoa_totaltmp.grup=tblcoagroup.group_id
    ";

    //print $SQL."<p>\n";
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    if ($MyResult) {
        foreach ($MyResult as $RowResult) {
                $LeftTree[$LeftIdx]["description"] = $RowResult["namacc"];
                $LeftIdx++;
                GetCOAList ($Space."      ", $RowResult["skada"], GetParam("m",""), "L");
                $LeftTree[$LeftIdx]["description"] = "TOTAL ".$RowResult["namacc"];
                $LeftTree[$LeftIdx]["aku_last"]    = $RowResult["aku_last"];
                $LeftTree[$LeftIdx]["aku_curr"]    = $RowResult["aku_curr"];
                $LeftTree[$LeftIdx]["mutasi"]      = $RowResult["mutasi"];
                $LeftTree[$LeftIdx]["lastyear"]    = $RowResult["lastyear"];
                $LeftTree[$LeftIdx]["inverse"]     = $Inverse;
                $LeftTree[$LeftIdx]["printing"]    = 1;
                $LeftIdx++;
                $LeftIdx++;

                $SUBTOTAL_LAST = 0;
                $SUBTOTAL_CURR = 0;
                $SUBTOTAL_MUT  = 0;

                $SUBTOTAL_LAST  = $RowResult["aku_last"];
                $SUBTOTAL_CURR  = $RowResult["aku_curr"];
                $SUBTOTAL_MUT   = $RowResult["mutasi"];
                $SUBTOTAL_LASTY = $RowResult["lastyear"];
        }

    }
}

function GetCOAList ($Space, $SkadaParent,$month,$orientation) {
    global $DBConnection, $LeftTree,$LeftIdx,$Inverse;
    if ($month<10) $month = "0".intval($month);

    if (!$LeftIdx)  $LeftIdx  = 0;
    $SQL = "
        SELECT skada, namacc, pathprn, parent, printing, jumlah, total, group_name, coatype, aku_last, aku_curr, mutasi, lastyear
        FROM tblcoa_totaltmp,tblcoagroup
        WHERE parent='$SkadaParent' AND tblcoa_totaltmp.grup=tblcoagroup.group_id AND printing=1
        ";
    //print $SQL."<br>\n\n";die();
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);

    if ($MyResult){
        foreach ($MyResult as $RowResult) {
                $LeftTree[$LeftIdx]["description"] = $Space.$RowResult["namacc"];
                $LeftTree[$LeftIdx]["aku_last"]    = $RowResult["aku_last"];
                $LeftTree[$LeftIdx]["aku_curr"]    = $RowResult["aku_curr"];
                $LeftTree[$LeftIdx]["mutasi"]      = $RowResult["mutasi"];
                $LeftTree[$LeftIdx]["lastyear"]    = $RowResult["lastyear"];

                $LeftTree[$LeftIdx]["printing"]    = $RowResult["printing"];
                $LeftTree[$LeftIdx]["inverse"]     = $Inverse;
                $LeftTree[$LeftIdx]["jumlah"]      = $RowResult["jumlah"];
                $LeftTree[$LeftIdx]["total"]       = $RowResult["total"];
                $totalleft = $LeftTree[$LeftIdx]["total"];
                $LeftIdx++;
                GetCOAList2 ($Space."      ",  $RowResult["skada"], $month, "L");
                if ($RowResult["jumlah"]==1) {
                  $LeftTree[$LeftIdx]["description"] = $Space."TOTAL ".$RowResult["namacc"];
                  $LeftTree[$LeftIdx]["aku_last"]    = $RowResult["aku_last"];
                  $LeftTree[$LeftIdx]["aku_curr"]    = $RowResult["aku_curr"];
                  $LeftTree[$LeftIdx]["mutasi"]      = $RowResult["mutasi"];
                  $LeftTree[$LeftIdx]["lastyear"]    = $RowResult["lastyear"];
                  $LeftTree[$LeftIdx]["inverse"]     = $Inverse;
                  $LeftTree[$LeftIdx]["printing"]    = 1;
                  $LeftIdx++;
                  $LeftIdx++;
                }


        }
    }
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return strtoupper(date("d F Y", mktime(0, 0, 0, $month+1, 0, $year)));

}

function GetCOAList2 ($Space, $SkadaParent,$month,$orientation) {
    global $DBConnection, $LeftTree,$LeftIdx,$Inverse;
    if ($month<10) $month = "0".intval($month);

    if (!$LeftIdx)  $LeftIdx  = 0;
    $SQL = "
        SELECT skada, namacc, pathprn, parent, printing, jumlah, total, group_name, coatype, aku_last, aku_curr, mutasi, lastyear
        FROM tblcoa_totaltmp,tblcoagroup
        WHERE parent='$SkadaParent' AND tblcoa_totaltmp.grup=tblcoagroup.group_id AND printing=1
        ";
    //print $SQL."<br>\n\n";die();
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);

    if ($MyResult){
        foreach ($MyResult as $RowResult) {

                $LeftTree[$LeftIdx]["description"] = $Space.$RowResult["namacc"];
                $LeftTree[$LeftIdx]["aku_last"]    = $RowResult["aku_last"];
                $LeftTree[$LeftIdx]["aku_curr"]    = $RowResult["aku_curr"];
                $LeftTree[$LeftIdx]["mutasi"]      = $RowResult["mutasi"];
                $LeftTree[$LeftIdx]["lastyear"]    = $RowResult["lastyear"];
                $LeftTree[$LeftIdx]["inverse"]     = $Inverse;
                $LeftTree[$LeftIdx]["printing"]    = $RowResult["printing"];
                $LeftTree[$LeftIdx]["jumlah"]      = $RowResult["jumlah"];
                $LeftTree[$LeftIdx]["total"]       = $RowResult["total"];
                $totalleft = $LeftTree[$LeftIdx]["total"];
                $LeftIdx++;
                GetCOAList ($Space."      ",  $RowResult["skada"], $month, "L");
                //$LeftTree[$LeftIdx]["printing"]    = 1;

        }
    }
}

class COAPDFReport extends PDF {
    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }

        $this->SetFont('arialn','',10);
            $this->Cell(200,5,$perush,0,1,'C',0);
            $this->Cell(200,5,$title,0,1,'C',0);
            if (GetParam("m","")=="AUDIT")
            $this->Cell(200,5,BuildPeriod (GetParam("y",""), 12),0,1,'C',0);
            else
            $this->Cell(200,5,BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
            $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
            $this->SetLeftMargin(5);
            $this->Line(5,30,210,30);
            $this->Ln();
            $this->Ln();
        //Header

        //Column widths
        $w=array(85,30,30,30,30);
        $this->SetFont('arialn','',10);
        $this->Cell($w[0],7,"DESKRIPSI",1,0,'C',0);
        $this->Cell($w[1],7,"S/D BLN LALU",1,0,'C',0);
        $this->Cell($w[2],7,"MUTASI",1,0,'C',0);
        $this->Cell($w[3],7,"S/D BLN INI",1,0,'C',0);
        $this->Cell($w[4],7,"LAST YEAR",1,0,'C',0);

        //$this->Cell($w[1],7,"LIABILITIES & EQUITY",1,0,'L',0);
        $this->Ln();
        $this->SetFont('arialn','',8);
    }

    function COATable() {
        global $LeftTree, $LeftIdx,$Inverse;
        //Column widths
        $w=array(85,30,30,30,30);

        //Data
        if ($LeftIdx>$RightIdx) $counter = $LeftIdx-2;
        else $counter = $RightIdx-2;

        for ($x=0;$x<=$counter;$x++)
        {

            $LDesc = $LeftTree[$x]["description"];
            if ($LeftTree[$x]["printing"]==1 && $LeftTree[$x]["jumlah"]<>1) {

                     if ($LeftTree[$x]["garis"]==1) {

                          //if ($LeftTree[$x]["aku_last"]<0)
                            $LeftTree[$x]["aku_last"] = $LeftTree[$x]["aku_last"]*-1;
                          //if ($LeftTree[$x]["aku_curr"]<0)
                            $LeftTree[$x]["aku_curr"] = $LeftTree[$x]["aku_curr"]*-1;
                          //if ($LeftTree[$x]["mutasi"]<0)
                            $LeftTree[$x]["mutasi"] = $LeftTree[$x]["mutasi"]*-1;
                          //if ($LeftTree[$x]["lastyear"]<0)
                            $LeftTree[$x]["lastyear"] = $LeftTree[$x]["lastyear"]*-1;

                      }

                      if ($LeftTree[$x]["inverse"]==1) {

                          //if ($LeftTree[$x]["aku_last"]<0)
                          $LeftTree[$x]["aku_last"] = $LeftTree[$x]["aku_last"]*-1;
                          //if ($LeftTree[$x]["aku_curr"]<0)
                          $LeftTree[$x]["aku_curr"] = $LeftTree[$x]["aku_curr"]*-1;
                          //if ($LeftTree[$x]["mutasi"]<0)
                          $LeftTree[$x]["mutasi"] = $LeftTree[$x]["mutasi"]*-1;
                          //if ($LeftTree[$x]["lastyear"]<0)
                          $LeftTree[$x]["lastyear"] = $LeftTree[$x]["lastyear"]*-1;
                      }
                          $Last_aku  = FormatNumber($LeftTree[$x]["aku_last"]);
                          $Curr_aku  = FormatNumber($LeftTree[$x]["aku_curr"]);
                          $Mutasi    = FormatNumber($LeftTree[$x]["mutasi"]);
                          $Last_Year = FormatNumber($LeftTree[$x]["lastyear"]);
            } else {
                $Last_aku  = "";
                $Curr_aku  = "";
                $Mutasi    = "";
                        $Last_Year = "";
            }
            if ($LeftTree[$x]["garis"]==1) {
                $this->Cell($w[0],5,$LDesc,"LRBT",0,'L',0);
                $this->Cell($w[1],5,$Last_aku,"LRBT",0,'R',0);
                $this->Cell($w[2],5,$Mutasi,"LRBT",0,'R',0);
                $this->Cell($w[3],5,$Curr_aku,"LRBT",0,'R',0);
                $this->Cell($w[4],5,$Last_Year,"LRBT",0,'R',0);
                $this->Ln();
            } else {
                $this->Cell($w[0],5,$LDesc,"LR",0,'L',0);
                $this->Cell($w[1],5,$Last_aku,"LR",0,'R',0);
                $this->Cell($w[2],5,$Mutasi,"LR",0,'R',0);
                $this->Cell($w[3],5,$Curr_aku,"LR",0,'R',0);
                $this->Cell($w[4],5,$Last_Year,"LR",0,'R',0);
                $this->Ln();
            }
        }
        //Closure line
        //$this->Cell(array_sum($w),0,'','T');
    }

    function Footer() {
        //Closure line
        $this->Cell(205,0,'','T');
    }

}

?>
